package com.gx.security.Symmetric_encryption;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.security.SecureRandom;
import java.security.Security;

/**
 * Created by gx on 2017/3/28.
 */
public class AESTest {
    private static String src = "imooc security AES";
    private static String password = "gaoxing";


    public static void main(String[] args) {
        jdkAEC();
        bcAEC();
        bcAECencryption();
    }

    public static void jdkAEC() {
        try {
            //初始化key  生成器
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            //初始化key生成长度
            keyGenerator.init(256);
            Key secretKey = keyGenerator.generateKey();
            //            拿到key
            byte[] byteskey = secretKey.getEncoded();

//            Key转化
            Key key = new SecretKeySpec(byteskey, "AES");
            //加密                          //加解密算法/工作模式/数据填充方式
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            //加密模式           转化后key值
            cipher.init(Cipher.ENCRYPT_MODE, key);
            //执行加密
            byte[] result = cipher.doFinal(src.getBytes());
//                                                            可以16进制也可以base64
            System.out.println("jdk Aes 加密结果encrypt:" + Base64.encodeBase64String(result));

            //解密操作       解密模式
            cipher.init(Cipher.DECRYPT_MODE, key);
//            拿到原始的result 解密
            result = cipher.doFinal(result);
            System.out.println("jdk Aes 解密结果decrypt:" + new String(result));
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public static void bcAEC() {
        try {
            Security.addProvider(new BouncyCastleProvider());
            //初始化key  生成器
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES", "BC");
            //初始化key生成长度
            keyGenerator.init(128);
            //            两种初始化        生成默认长度key   设置默认key
//            new SecureRandom(passwword.getbytes());
//            keyGenerator.init(new SecureRandom(password.getbytes()));

//            两种方式设置种子
//            SecureRandom secureRandom=new SecureRandom();
//            secureRandom.setSeed(password.getbytes());
//            keyGenerator.init(secureRandom);


            Key secretKey = keyGenerator.generateKey();
            //            拿到key
            byte[] byteskey = secretKey.getEncoded(); /// 返回基本编码格式的密钥，如果此密钥不支持编码，则返回

//            Key转化
            Key key = new SecretKeySpec(byteskey, "AES");// 转换为AES专用密钥
            //加密                          //加解密算法/工作模式/数据填充方式
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            //加密模式           转化后key值
            cipher.init(Cipher.ENCRYPT_MODE, key);
            //执行加密
            byte[] result = cipher.doFinal(src.getBytes());
//                                                            可以16进制也可以base64
            System.out.println("bc Aes 加密结果encrypt:" + Base64.encodeBase64String(result));

            //解密操作       解密模式
            cipher.init(Cipher.DECRYPT_MODE, key);
//            拿到原始的result 解密
            result = cipher.doFinal(result);
            System.out.println("bc Aes 解密结果decrypt:" + new String(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    public static void bcAECencryption() {
        try {
            Security.addProvider(new BouncyCastleProvider());
            //初始化key  生成器
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES", "BC");
            //初始化key生成长度
//            keyGenerator.init(128);
            //            两种初始化        生成默认长度key   设置默认key
            new SecureRandom(password.getBytes());
            keyGenerator.init(new SecureRandom(password.getBytes()));

//            两种方式设置种子
//            SecureRandom secureRandom=new SecureRandom();
//            secureRandom.setSeed(password.getbytes());
//            keyGenerator.init(secureRandom);


            Key secretKey = keyGenerator.generateKey();
            //            拿到key
            byte[] byteskey = secretKey.getEncoded(); /// 返回基本编码格式的密钥，如果此密钥不支持编码，则返回

//            Key转化
            Key key = new SecretKeySpec(byteskey, "AES");// 转换为AES专用密钥
            //加密                          //加解密算法/工作模式/数据填充方式
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            //加密模式           转化后key值
            cipher.init(Cipher.ENCRYPT_MODE, key);
            //执行加密
            byte[] result = cipher.doFinal(src.getBytes());
//                                                            可以16进制也可以base64
            System.out.println("bc Aes 加密结果encrypt:" + Base64.encodeBase64String(result));

            //解密操作       解密模式
            cipher.init(Cipher.DECRYPT_MODE, key);
//            拿到原始的result 解密
            result = cipher.doFinal(result);
            System.out.println("bc Aes 解密结果decrypt:" + new String(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
